#include "Inf_Buzzer.h"

static void Inf_Buzzer_Start(void)
{
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
}

static void Inf_Buzzer_Stop(void)
{
    HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
}

int8_t Inf_Buzzer_Sound(uint32_t freq, uint8_t Duty, uint16_t time)
{
    if(freq > 1000000)
    {
        return -1;
    }
    if(Duty > 100)
    {
        return -1;
    }
    __HAL_TIM_SetCounter(&htim1, 1000000 / freq - 1);
    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, (1000000 / freq) * (Duty / 100.0));
    Inf_Buzzer_Start();
    HAL_Delay(time);
    Inf_Buzzer_Stop();
    return 0;
}
